/**
 * Copyright (c) 2005-2010 springside.org.cn Licensed under the Apache License,
 * Version 2.0 (the "License"); $Id: SpringContextHolder.java 1211 2010-09-10
 * 16:20:45Z calvinxiu $
 */
package com.dmagic.cms.common.spring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * static save Spring ApplicationContext, get ApplicaitonContext in everywhere.
 * 
 * @author songlin.li
 */
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {

    private static ApplicationContext applicationContext = null;

    private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);

    /**
     * implements ApplicationContextAware interface, injection Context in static
     * variable.
     */
    public void setApplicationContext(ApplicationContext applicationContext) {
        logger.debug("injection ApplicationContext in SpringContextHolder:" + applicationContext);

        if (SpringContextHolder.applicationContext != null) {
            logger.warn("SpringContextHolder ApplicationContext has been replace,inhere ApplicationContext:"
                    + SpringContextHolder.applicationContext);
        }

        SpringContextHolder.applicationContext = applicationContext; // NOSONAR
    }

    /**
     * implements DisposableBean interface, close Context and clean static
     * variable.
     */
    public void destroy() throws Exception {
        SpringContextHolder.clear();
    }

    /**
     * get static variable from ApplicationContext.
     */
    public static ApplicationContext getApplicationContext() {
        assertContextInjected();
        return applicationContext;
    }

    /**
     * get bean from applicationContext.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        assertContextInjected();
        return (T) applicationContext.getBean(name);
    }

    /**
     * get bean from applicationContext.
     */
    public static <T> T getBean(Class<T> requiredType) {
        assertContextInjected();
        return applicationContext.getBean(requiredType);
    }

    /**
     * clean ApplicationContext to null at SpringContextHolder.
     */
    public static void clear() {
        logger.debug("clean ApplicationContext to null at SpringContextHolder:" + applicationContext);
        applicationContext = null;
    }

    /**
     * check not null ApplicationContext.
     */
    private static void assertContextInjected() {
        if (applicationContext == null) {
            throw new IllegalStateException(
                    "applicaitonContext has not injection,please defined SpringContextHolder in applicationContext.xml");
        }
    }
}
